package com.hzsxy.bighomework.controller;

import com.hzsxy.bighomework.api.ExamTotalView;
import com.hzsxy.bighomework.entity.*;
import com.hzsxy.bighomework.repository.ClassInfoRepository;
import com.hzsxy.bighomework.repository.ExamClassRepository;
import com.hzsxy.bighomework.repository.ExamRepository;
import com.hzsxy.bighomework.repository.StudentClassRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/exam")
public class ExamController {
    @Autowired
    private ExamRepository examRepository;
    @Autowired
    private ClassInfoRepository classInfoRepository;
    @Autowired
    private ExamClassRepository examClassRepository;
    @Autowired
    private StudentClassRepository studentClassRepository;

    @GetMapping("/total")
    public String getTotal(Model model,HttpSession httpSession){
        String type = (String) httpSession.getAttribute("type");
        if ("student".equals(type)){
            StudentDO studentDO = (StudentDO) httpSession.getAttribute("info");
            List<StudentClassDO> studentClassDOList = studentClassRepository.findByStudentId(studentDO.getStudentId());
            List<ExamTotalView> viewList = new ArrayList<>();
            Map<Integer,Integer> existMap = new HashMap<>();
            studentClassDOList.forEach(it -> {
                List<ExamClassDO> viewClassDOList = examClassRepository.findByClassId(it.getClassId());
                List<ExamTotalView> tempView = viewClassDOList.stream().filter( n -> {
                    if (existMap.get(n.getExamId())!=null && existMap.get(n.getExamId())!=0){
                        return false;
                    }else {
                        existMap.put(n.getExamId(),1);
                        return true;
                    }
                }).map( n -> {
                    ExamDO examDO = examRepository.findOne(n.getExamId());
                    ExamTotalView view = new ExamTotalView();
                    view.setId(examDO.getId());
                    view.setAuthor(examDO.getAuthor());
                    view.setName(examDO.getName());
                    view.setTime(examDO.getExamTime().toString());
                    return view;
                }).collect(Collectors.toList());
                viewList.addAll(tempView);
            });
            model.addAttribute("views",viewList);
            return "student/exam-total";
        }else if("teacher".equals(type)){
            Teacher teacher = (Teacher) httpSession.getAttribute("info");
            List<ExamDO> examDOList = examRepository.findByAuthor(teacher.getName());
            List<ExamTotalView> viewList = examDOList.stream().map(it -> {
                ExamTotalView view = new ExamTotalView();
                view.setId(it.getId());
                view.setAuthor(it.getAuthor());
                view.setName(it.getName());
                view.setTime(it.getExamTime().toString());
                return view;
            }).collect(Collectors.toList());
            model.addAttribute("views",viewList);
            return "teacher/exam-total";
        } else {
            List<ExamDO> examDOList = (List<ExamDO>) examRepository.findAll();
            List<ExamTotalView> viewList = examDOList.stream().map(it -> {
                ExamTotalView view = new ExamTotalView();
                view.setId(it.getId());
                view.setAuthor(it.getAuthor());
                view.setName(it.getName());
                view.setTime(it.getExamTime().toString());
                return view;
            }).collect(Collectors.toList());
            model.addAttribute("views",viewList);
            return "teacher/exam-total";
        }
    }

    @RequestMapping("/teacher/remove")
    @ResponseBody
    public String remove(@RequestParam int examId){
        try{
            examRepository.delete(examId);
            return "succeeded";
        }catch (Exception ex){
            ex.printStackTrace();
            return "failed";
        }
    }

    @RequestMapping("/teacher/addPage")
    public String addPage(Model model){
        List<ClassInfoDO> classInfoDOList = classInfoRepository.findAll();
        model.addAttribute("classes",classInfoDOList);
        return "teacher/exam-add";
    }

    @RequestMapping("/teacher/add")
    @ResponseBody
    public String add(HttpServletRequest request, HttpSession httpSession){
        try{
            Teacher teacher = (Teacher) httpSession.getAttribute("info");
            String name = request.getParameter("name");
            List<String> classIdList = Arrays.asList(request.getParameterValues("classIds[]"));
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            String examTime = request.getParameter("examTime");
            ExamDO examDO = new ExamDO();
            examDO.setName(name);
            examDO.setExamTime(simpleDateFormat.parse(examTime));
            examDO.setAuthor(teacher.getName());
            ExamDO exam = examRepository.save(examDO);
            classIdList.forEach(it -> {
                ExamClassDO examClassDO = new ExamClassDO();
                examClassDO.setClassId(it);
                examClassDO.setExamId(exam.getId());
                examClassRepository.save(examClassDO);
            });
            return "succeeded";
        }catch (Exception ex){
            ex.printStackTrace();
            return "failed";
        }
    }
}
